/*
 * CN_PARTY_LEAVE.cpp
 *
 *  Created on: 2013. 5. 21.
 *      Author: the2
 */


#include "../../User.h"
#include "dungeon.h"

#include "../../NLogic/Space.h"
#include "../../NLogic/Battle.h"
#include "../../NLogic/Function.h"
#include "../../NLogic/Party.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand { namespace NDungeon {

void CN_PARTY_LEAVE::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	NLogic::Space *pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("CN_PARTY_LEAVE NULL == pSpace");
		return;
	}

	if (false == pSpace->IsDungeon())
	{
		DEBUGLOG("CN_PARTY_LEAVE space is not single or party dungeion");
		return;
	}


	PREPARE_PACKET;
	int party_seq = 0;
	pPacket->ParseMsg("%d", & party_seq);

	if (true == pSpace->IsSingleDungeon())
	{
		processDungeon(pUser, pPacket);
		return;
	}

	if (true == pSpace->IsPartyDungeon())
	{
		processDungeon(pUser, pPacket);
		return;
	}

//
//	if( pSpace->bDungeonStart == false )
//	{
//		//
//		pUser->player_info.x = pUser->player_info.old_x;
//		pUser->player_info.y = pUser->player_info.old_y;
//		pUser->player_info.degree = pUser->player_info.old_degree;
//
//		//
//		pSpace->LeavePlayer(pUser);
//
//		//
//		WorldPlayerAdd(pUser);
//
//		//return;
//	}
//	else	// 백키 터치시 패널티
//	{
//		// 던전에서 획득한 골드 및 경험치
//		int gold = pSpace->pDungeonResultPersonal[pUser->GetGID()]->get_gold;
//		int exp = pSpace->pDungeonResultPersonal[pUser->GetGID()]->hunting_exp;
//
//		::Logger::GETLOG()->ToFile(::Logger::DEBUG, "CN_PARTY_LEAVE : GID=%d, get_gold=%d, get_exp=%d", pUser->GetGID(), gold, exp);
//
//		if (gold > 0)
//		{
//			gold = NUtil::AddPercentage(gold, DUNGEON_LEAVE_PENALTY_GOLD);
//			::Logger::GETLOG()->ToFile(::Logger::DEBUG, "CN_PARTY_LEAVE : penalty_gold=%d", gold);
//			pUser->GetCharacterInfo().gold += gold;
//		}
//
//		if (exp > 0)
//		{
//			exp = NUtil::AddPercentage(exp, DUNGEON_LEAVE_PENALTY_EXP);
//			::Logger::GETLOG()->ToFile(::Logger::DEBUG, "CN_PARTY_LEAVE : penalty_exp=%d", exp);
//			pUser->GetLevel()->IncreaseExp(exp);
//		}
//
//	}
//
//	//
//	if( pSpace->SetNewChief(pUser) != 0 )
//		SendPartyLeave(pUser, pSpace->GetChief()->GetGID());
}

//void CN_PARTY_LEAVE::SendPartyLeave(User *pUser, int chief_gid)
//{
//	::ProjectZ::NGame::CBaseSpace *pSpace = pUser->GetSpace();
//
//	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
//	pNoti->MsgCat("%d%d", pUser->GetGID(), chief_gid);
//	pNoti->MakePacket(CMD_SN_PARTY_LEAVE);
//	pSpace->SendToMessageExceptMe(pNoti, pUser->GetGID());
//}

void CN_PARTY_LEAVE::processDungeon(User * pUser, ::Network::Packet * pPacket)
{
	// 던전에서 획득한 골드 및 경험치
	int gold = pUser->GetBattle()->GetData().get_gold;
	int exp = pUser->GetBattle()->GetData().hunting_exp;

	NResource::BalanceData * pBalance = NResource::Static::InstancePtr()->GetBalance()->GetBalancePtr(pUser);
	if (NULL == pBalance)
		return;

	if (gold > 0)
	{
		gold = (gold * pBalance->GetValue(EM_DUNGEON_PENALTY_GOLD)) / 100;
		NLogic::NFunction::ModifyGold(pUser, gold);
	}

	if (exp > 0)
	{
		exp = (exp * pBalance->GetValue(EM_DUNGEON_PENALTY_EXP)) / 100;
		ProjectZ::NLogic::NFunction::IncreaseExp(pUser, exp);
	}
//
	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
	pNoti->MsgCat("%d%d", pUser->GetUserSeq(), 0);
	pNoti->MakePacket(CMD_SN_PARTY_LEAVE);
	pUser->Send(pNoti);

	if (NULL != pUser->GetParty())
	{
		pUser->GetParty()->LeaveUser(pUser);
	}
}


} /*NDungeon*/ } /*NCommand*/ } /*ProjectZ*/
